package com.app.studyroom.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialog;

public class CustomSizeDialog extends AppCompatDialog {

    private final float widthPercent;
    private final float heightPercent;
    private final int layoutResId;

    public CustomSizeDialog(@NonNull Context context, int layoutResId, float widthPercent, float heightPercent) {
        super(context);
        this.layoutResId = layoutResId;
        this.widthPercent = widthPercent;
        this.heightPercent = heightPercent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutResId);

        // 设置窗口大小
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        lp.width = (int) (metrics.widthPixels * widthPercent);
        lp.height = (int) (metrics.heightPixels * heightPercent);
        getWindow().setAttributes(lp);

        // 可选：设置背景透明圆角
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }
}
